package com.fy.service.server.listener;

import com.alibaba.fastjson.JSONObject;
import com.fy.common.constant.CmdConstants;
import com.fy.common.model.im.MessageModel;
import com.fy.service.service.ImBrokerGroupService;
import com.fy.service.service.ImBrokerService;
import com.fy.service.service.MessageService;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * Description 消息路由中心
 */

@Component
public class BrokerMessageListener implements MessageListenerConcurrently {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ImBrokerService imBrokerService;

    @Autowired
    private ImBrokerGroupService imBrokerGroupService;
    @Autowired
    private MessageService messageService;
/**
     * 消息通用路由中心 再查找用户最后一次连接的tcp节点 通知下发中心下发
     * @param list
     * @param consumeConcurrentlyContext
     * @return
     */

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        try {
            for (MessageExt messageExt : list) {

                byte[] body = messageExt.getBody();
                String msg = new String(body);
                //
                MessageModel model = JSONObject.parseObject(msg,MessageModel.class);
                //已收到消息需要走批量下发逻辑单独调用处理方法

                switch (model.getCmd()) {
                    //下发单聊消息
                    case CmdConstants.REQUEST_SINGLE_CHAT:
                        model.setCmd(CmdConstants.RESPONSE_SINGLE_CHAT);
                        imBrokerService.sendSingleMessage(model);
                        break;
                    case CmdConstants.REQUEST_TYPING:
                        model.setCmd(CmdConstants.RESPONSE_TYPING);
                        imBrokerService.sendTyping(model);
                        break;
                    //
                    case CmdConstants.REQUEST_RECALL:
                        model.setCmd(CmdConstants.RESPONSE_RECALL);
                        imBrokerService.sendRecall(model);
                        break;
                    //消息已读
                    case CmdConstants.REQUEST_READ_MESSAGE:
                        model.setCmd(CmdConstants.RESPONSE_READ_MESSAGE);
                        imBrokerService.readMessage(model);
                        break;
                    case CmdConstants.REQUEST_READ_DESTROY:
                        model.setCmd(CmdConstants.RESPONSE_READ_DESTROY);
                        imBrokerService.sendSingleMessage(model);
                        break;
                    //发送群聊消息
                    case CmdConstants.REQUEST_GROUP_CHAT:
                        model.setCmd(CmdConstants.RESPONSE_GROUP_CHAT);
                        imBrokerGroupService.sendGroupMessage(model,true);
                        break;
                    //群成员添加通知
                    case CmdConstants.RESPONSE_ADD_GROUP:
                        model.setCmd(CmdConstants.RESPONSE_ADD_GROUP);
                        imBrokerGroupService.sendGroupMessage(model,false);
                        break;
                    //阅后即焚已读
                    case CmdConstants.REQUEST_READ_DESTROY_READ:
                        model.setCmd(CmdConstants.RESPONSE_READ_DESTROY_READ);
                        messageService.saveFromMessage(model);
                        break;
                    //消息删除
                    case CmdConstants.REQUEST_DELETE_MESSAGE:
                        model.setCmd(CmdConstants.RESPONSE_DELETE_MESSAGE);
                        imBrokerService.sendSingleMessage(model);
                        break;
                    default:
                        //通用默认下发方法
                        //imBrokerService.commonAddressingMessage(model);
                }

            }
        } catch (Exception e) {
            logger.error("message transpond error " ,e);
        }

        //执行完成消费成功
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

    }

}
